<?php
class tls_amf {

	/**
	 * 分析请求 获取请求参数
	 * @return void
	**/
	public static function parseRequest() {

		require_once THIRD_PATH . 'Zend/Amf/Request/Http.php';
		$req = new Zend_Amf_Request_Http();

		$requestEncoding	= $req->getObjectEncoding();
		$requestHeader	  = $req->getAmfHeaders();
		$requestBodies	  = $req->getAmfBodies();
		unset($req);

		$return = false;
		if(empty($requestBodies)) return $return;

		try {
			$body	 = $requestBodies[0];
			$target   = $body->getTargetUri();

			list($_class, $_action) = array_pad(explode('.', $target, 2), 2, '');

			$data = $body->getData();

			$data = ( isset($data[0]) && is_object($data[0]) ) ? get_object_vars($data[0]) : $data[0];

			$responseURI = $body->getResponseURI() . Zend_AMF_Constants::RESULT_METHOD;
			$return = array(
				'responseURI'	=> $responseURI,
				'controller'	=> strtolower($_class),
				'action'		=> strtolower($_action),
				'data'			=> $data
			);
		} catch(exception $e) {

		}

		return $return;
	}

	/**
	 * 分析内容 返回AMF格式给flash
	 * @param $responseURI
	 * @param $data
	 * @return void
	**/
	public static function parseResponse($responseURI, $data) {

		require_once THIRD_PATH . 'Zend/Amf/Response/Http.php';
		$res = new Zend_Amf_Response_Http();

		$res->addAmfBody(new Zend_Amf_Value_MessageBody($responseURI, null, $data));
		$res->finalize();
		return $res->getResponse();
	}

}